package com.xuhai.blog.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuhai.blog.admin.entity.CategoryEntity;
import com.xuhai.blog.admin.mapper.CategoryMapper;
import com.xuhai.blog.admin.service.ArticleCategoryService;
import com.xuhai.blog.admin.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuhai.blog.common.exception.RTException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 文章分类表 服务实现类
 * </p>
 *
 * @author 机蜂
 * @since 2022-02-08
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryEntity> implements CategoryService {

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public IPage<CategoryEntity> getPage(int page, int limit, CategoryEntity category) {
        return this.page(new Page<>(page,limit)
                ,new QueryWrapper<CategoryEntity>()
                        .eq(StringUtils.isNotBlank(category.getCategoryName()),"category_name",category.getCategoryName())
                        .eq(category.getStatus() != null,"status",category.getStatus())
                        .orderByDesc("category_id")
        );
    }

    @Override
    public void deleteInfo(Integer categoryId) {
        if(articleCategoryService.getArticleByCategoryId(categoryId).size()>0){
            throw new RTException("该分类已被使用，无法删除！");
        }
        this.removeById(categoryId);
    }

    @Override
    public List<CategoryEntity> select() {
        return this.list(new QueryWrapper<CategoryEntity>().eq("status",1));
    }

    @Override
    public int getCount(Integer status) {
        return this.count(new QueryWrapper<CategoryEntity>().eq(status!=null,"status",status));
    }

    @Override
    public List<CategoryEntity> getPieData() {
        return categoryMapper.getCategoryPieData();
    }
}
